package com.heima.reggie.filter;

import com.heima.reggie.bean.Employee;
import com.heima.reggie.bean.User;
import com.heima.reggie.utils.BaseContext;
import sun.management.Agent;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//@WebFilter("/*")
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
       /* HttpServletRequest  request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;

        String uri = request.getRequestURI();

        if(uri.contains("login") || uri.endsWith(".css") || uri.endsWith(".js")
                || uri.contains("images") || uri.contains("plugin") || uri.contains("style")
                || uri.endsWith(".ico")){
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        Employee employee = (Employee) request.getSession().getAttribute("employee");
        if (employee != null) {
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        response.sendRedirect("/backend/page/login/login.html");*/
        //0. 转化对象
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;


        //1. 获取请求地址   /backend/index.html
        String uri = req.getRequestURI();

        //System.out.println("被抓住的请求是：" + uri);


        //2. 判定是否属于直接放行的请求
        if(uri.contains("login") || uri.endsWith(".css") || uri.endsWith(".js")
                || uri.contains("images") || uri.contains("plugin") || uri.contains("style")
                || uri.endsWith(".ico")||uri.contains("sms")){
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        //如果是后端路径进入if
        if (req.getHeader("User-Agent").contains("Windows")){
            //3. 如果没有进入上面的if， 即表示要判定登录
            //3.1 从session作用域里面获取登录的时候，保存的员工对象
            Employee employee = (Employee) req.getSession().getAttribute("employee");

            //3.2 判定是否有值
            if(employee != null){ //表示已经登录了
              /*  System.out.println("---------------------");
                System.out.println(employee);*/
                BaseContext.setCurrentId(employee.getId());
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
            resp.sendRedirect("/backend/page/login/login.html");
        }else{
            //3.3 从session作用域里面获取登录的时候，保存的用户对象
            User user = (User) req.getSession().getAttribute("user");
            //3.4 判定是否有值
            if(user != null){ //表示已经登录了

                BaseContext.setCurrentId(user.getId());
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
            resp.sendRedirect("/front/page/login.html");
        }

      /*
        //4. 如果没有进入上面的if，即表示该请求，不属于直接放行的请求，也没有登录。要跳转登录页面

        if (uri.contains("backend")){

        }else{

        }*/


    }
}
